//
//  PersonHeadView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/5.
//  Copyright © 2016年 game. All rights reserved.
//

#import "PersonHeadView.h"
#import "MyConmentCtrl.h"
#import "MyInformController.h"
#import "SDImageCache.h"
#import "MyCollectViewController.h"
#import "LoginController.h"
#import "My_threadViewController.h"
#import "RKNotificationHub.h"
@interface PersonHeadView()
@property (nonatomic,strong) AppDelegate *app;

@property (nonatomic,strong) RKNotificationHub *Mynewshub;

@property (nonatomic,strong) RKNotificationHub *commenthub;

@property (nonatomic,strong) RKNotificationHub *addfrindhub;

@property (nonatomic,strong) RKNotificationHub *systemhub;
@end
@implementation PersonHeadView

-(RKNotificationHub *)Mynewshub{
    if (!_Mynewshub) {
        _Mynewshub = [[RKNotificationHub alloc]initWithView:_footView.comment];
    }
    
    return _Mynewshub;
}

-(RKNotificationHub *)commenthub{

    if (!_commenthub) {
        _commenthub = [[RKNotificationHub alloc]initWithView:_footView.replay];
    }
    
    return _commenthub;
}

-(RKNotificationHub *)addfrindhub{

    if (!_addfrindhub) {
        _addfrindhub = [[RKNotificationHub alloc]initWithView:_footView.pramis];
    }
    return _addfrindhub;
}

-(RKNotificationHub *)systemhub{

    if (!_systemhub) {
        _systemhub = [[RKNotificationHub alloc]initWithView:_footView.news];
    }
    
    return _systemhub;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self UserVIew];
        [self CentreView];
        [self FootView];
        self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    }
    return self;
}

-(void)setModel:(UserDataModel *)model{

    _model = model;
    self.userView.model = _model;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if ([self.app.isLogin isEqualToString:@"YES"]) {
            
            self.Mynewshub.count = _model.pm_members_count.intValue;
            self.commenthub.count = _model.mypost_notify_count.intValue;
            self.addfrindhub.count = _model.interactive_notify_count.intValue;
            self.systemhub.count = _model.system_notify_count.intValue;
            
            int count = _model.pm_members_count.intValue+_model.mypost_notify_count.intValue+ _model.interactive_notify_count.intValue+_model.system_notify_count.intValue;
            
                if (count==0&&[self.app.isLogin isEqualToString:@"YES"]) {
                    
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         if (self.app.userModel) {
                              [[NSNotificationCenter defaultCenter]postNotificationName:@"LoginNotifi" object:nil userInfo:@{@"sss":self.app.userModel}];//发送通知
                         }
                     });
                }
        }
    });
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    [self setHUDLocalWhithHud:self.Mynewshub];
    [self setHUDLocalWhithHud:self.commenthub];
    [self setHUDLocalWhithHud:self.addfrindhub];
    [self setHUDLocalWhithHud:self.systemhub];
}

-(void)setHUDLocalWhithHud:(RKNotificationHub *)hud{

    [hud moveCircleByX:10 Y:-5];
    [hud scaleCircleSizeBy:0.7];
}


-(void)UserVIew{

    
    self.userView = [[NSBundle mainBundle]loadNibNamed:@"PersonHeadUserView" owner:nil options:nil].lastObject;
    self.userView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.2);
    
    [self addSubview:self.userView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTap:)];
    [self.userView addGestureRecognizer:tap];
}

/**
 *  点击登录/点击查看个人资料
 */
-(void)didTap:(UITapGestureRecognizer *)tap{
    
    
    if ([self.deleget respondsToSelector:@selector(InformClickTop)]) {
        [self.deleget InformClickTop];
    }
    
}

-(void)CentreView{
    
    _centerView = [[NSBundle mainBundle]loadNibNamed:@"PsonHeadCertenView" owner:nil options:nil].lastObject;
   
    _centerView.frame = CGRectMake(0, Height(_userView), SCREEN_WIDTH,  SCREEN_HEIGHT*0.2);
    [self addSubview:_centerView];
    
    [_centerView.tiezi addTarget:self action:@selector(MyOne:) forControlEvents:UIControlEventTouchUpInside];
    [_centerView.dianping addTarget:self action:@selector(MyOne:) forControlEvents:UIControlEventTouchUpInside];
    [_centerView.shoucang addTarget:self action:@selector(MyOne:) forControlEvents:UIControlEventTouchUpInside];
    [_centerView.footShow addTarget:self action:@selector(footShowClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)footShowClick:(UIButton *)button{

    if ([self.deleget respondsToSelector:@selector(InformClickTop)]) {
        [self.deleget InformClickTop];
    }

}
/**
 * 我发布的帖子  我发表的点评  我的收藏点击事件
 *
 * tag值从10开始
 */
-(void)MyOne:(UIButton *)sender{
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        if ((sender.tag-10)==0) {
            My_threadViewController *myThread = [My_threadViewController new];
            [self.farherCtrl.navigationController pushViewController:myThread animated:YES];
            
        }else if ((sender.tag-10)==1) {
            MyConmentCtrl *comment = [MyConmentCtrl new];
            [self.farherCtrl.navigationController pushViewController:comment animated:YES];
        }else if ((sender.tag-10)==2){
            MyCollectViewController *collect = [MyCollectViewController new];
            
            [self.farherCtrl.navigationController pushViewController:collect animated:YES];
        }

    }else{
    
        LoginController *login = [LoginController new];
     [self.farherCtrl.navigationController pushViewController:login animated:YES];
    }
}

-(void)FootView{
    
    self.footView = [[NSBundle mainBundle]loadNibNamed:@"PersonUserFootView" owner:nil options:nil].lastObject;
    
    self.footView.frame = CGRectMake(0, Height(_centerView), SCREEN_WIDTH,SCREEN_HEIGHT*0.2);
    
    [self addSubview:self.footView];
    
    [_footView.comment addTarget:self action:@selector(footClick:) forControlEvents:UIControlEventTouchUpInside ];
     [_footView.replay addTarget:self action:@selector(footClick:) forControlEvents:UIControlEventTouchUpInside ];
     [_footView.pramis addTarget:self action:@selector(footClick:) forControlEvents:UIControlEventTouchUpInside ];
     [_footView.news addTarget:self action:@selector(footClick:) forControlEvents:UIControlEventTouchUpInside ];
}

/**
 *  评论   回复  被赞  系统消息  点击事件
 *
 *  tag值从100开始
 */
-(void)footClick:(UIButton *)btn{
    
    
    NSInteger count = btn.tag-100;
    [self.deleget NewsClickWhithTag:btn.tag-100];
    
    switch (count) {
        case 0:
            
            self.app.userModel.pm_members_count = @"0";
            break;
            
        case 1:
            
            self.app.userModel.mypost_notify_count = @"0";
            break;
            
        case 3:
            self.app.userModel.system_notify_count = @"0";
            
            break;
            
        default:
            break;
    }

}

@end
